package codeRandomThoughts.Test45跳跃游戏II;

public class Solution {

    public int jump(int[] nums) {
        int index = 0;
        int count = 0;
        if(nums.length==1){
            return 0;
        }

        while (nums[index] != 0) {
            //首先获取能跳到的范围
            int range = nums[index];
            int nextIndex = index;
            int maxDistance = index + range;

            //如果当前能跳到终点,返回true
            if (maxDistance + 1 >= nums.length) {
                return count+1;
            }

            for (int i = 1; i <= range; i++) {
                int jumpArea = index + i + nums[index + i];
                if (jumpArea > maxDistance) {
                    maxDistance = jumpArea;
                    nextIndex = index + i;
                }
            }

            index = nextIndex;
            count++;
        }

        return count;
    }

}

